// 导入 mongoose
const { default: mongoose } = require("mongoose");

// 连接数据库
mongoose.connect("mongodb://127.0.0.1:27017/bilibili");

// 回调设置
mongoose.connection.once("open", () => {
  // 创建文档结构
  let bookSchema = new mongoose.Schema({
    name: String,
    author: String,
    price: Number,
    is_hot: Boolean,
  });

  // 创建文档结构的对象
  let bookModel = mongoose.model("novel", bookSchema);

  // 删除文档
  // 单个文档
  /* bookModel.deleteOne(
    {
      // _id: "66139dad6e095709840bcb11",
      name: "哈利波特",
      // is_hot: true,
    },
    (err, data) => {
      if (err) {
        console.log(err);
        return;
      }
      console.log(data);
    },
  ) */;

  // 批量删除
  bookModel.deleteMany(
    {
      is_hot: true,
    },
    (err, data) => {
      if (err) {
        console.log(err);
        return;
      }
      console.log(data);
    },
  );

  console.log("连接成功");
});

mongoose.connection.on("error", () => {
  console.log("连接失败");
});

mongoose.connection.on("close", () => {
  console.log("连接关闭");
});
